package net.kts.attraction.search;


import java.util.ArrayList;
import java.util.List;

import net.kts.attraction.search.model.Address;
import net.kts.attraction.search.model.YahooAddress;
import net.kts.attraction.subway.adapter.AttractionAddressAdapter;
import net.kts.main.R;
import net.kts.transrate.TranslateService;
import net.kts.util.network.NetworkCheck;
import net.kts.util.url.RequestURL;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class AttractionAddressList extends Activity {

	private String searchText = null;
	private YahooAddress yahooAddress = null;
	private OnItemClickListener onItemClickListener = null;
	private ListView listView = null;
	private ProgressDialog progressDialog = null;
	private Handler uiCallback = null;
	private Thread runSetting = null;
	private ArrayAdapter<String> adapterList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attraction_address_list);

		



			progressDialog = new ProgressDialog(this);//ProgressDialog.show(this, "Request AddressList", "search Address...");
			progressDialog.setMessage("Address Searching...");
			progressDialog.show();


			itemClickInit();
			handlerInit();

			this.listView = (ListView)findViewById(R.id.addressListView);
			this.listView.setOnItemClickListener(onItemClickListener);
			this.searchText = getIntent().getStringExtra("search");


		

		//		setting();
	}

	




	private void requestThreadInit() {
		this.runSetting = new Thread() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				//				try {
				//					Thread.sleep(100);
				//				} catch (InterruptedException e) {
				//					// TODO Auto-generated catch block
				//					e.printStackTrace();
				//				}

				adapterInit();				
				uiCallback.sendEmptyMessage(0);
			}

		};
	}

	private void handlerInit() {



		this.uiCallback = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);

				setting();

			}

		};

	}

	private void itemClickInit() {
		this.onItemClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> items, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				Address address = yahooAddress.getAddresseList().get(position);
				if(Integer.valueOf(yahooAddress.getHead().getFound().trim()) != 0) {

					//					Toast.makeText(getBaseContext(), address.getAddressFullName(), Toast.LENGTH_SHORT).show();



					Intent intent = new Intent(getBaseContext(), AttractionUserSelect.class);

					Bundle bundle = new Bundle();
					bundle.putSerializable("address", address);
					intent.putExtras(bundle);

					//					intent.putExtra("name", address.getAddressFullName().trim());
					//					intent.putExtra("city", address.getCity().trim());
					//					intent.putExtra("lat", address.getLatitude().trim());
					//					intent.putExtra("lng", address.getLongitude().trim());
					//					intent.putExtra("trans", address.getTransText());

					startActivity(intent);

				}


			}
		};

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		requestThreadInit();
		this.runSetting.start();

	}




	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

	}

	private void adapterInit() {
		List<String> strList = new ArrayList<String>();


		String result = TranslateService.translateToKorean(this.searchText);
		result = result.replaceAll(" ", "");

		this.yahooAddress = RequestURL.requestYahooAddress(result);
		String found = this.yahooAddress.getHead().getFound().trim();

		if(Integer.valueOf(found) == 0) {
			strList.add("Search Not Found!||"+TranslateService.translate("Search Not Found!"));

		}

		else {
			for(Address address : this.yahooAddress.getAddresseList()) {

				String language = TranslateService.translateRepository(address.getAddressFullName().trim());
				address.setTransText(language);

				strList.add(address.getAddressFullName().trim() + "||(" +language+ ")" );
			}
		}			

		this.adapterList = new AttractionAddressAdapter(this, R.layout.attraction_address_list, strList);
	}


	private void setting() {

		listView.setAdapter(adapterList);

		this.progressDialog.dismiss();
	}





}
